import {formatTime} from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopItem:{},
    oldPrice:0,
    allPrice:0,
    count:1,
    num:1,
    temp:1,
    sugar:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.showItem(options)
  },
  //改变温度
  changTemp(event) {
    let status = parseInt(event.target.dataset.index);
    console.log(status)
    this.setData({
      temp:status
    })
  },
  //改变甜度
  changSugar(event){
    let status = parseInt(event.target.dataset.index);
    console.log(status)
    this.setData({
      sugar:status
    })
  },
  //加载商品的数据
  showItem(options){
    let id = parseInt(options.id);
    wx.cloud.callFunction({
      name:"getShopDesc",
      data:{
        id:id
      }
    }).then(res => {
      this.setData({
        shopItem:res.result.data,
        temp:res.result.data[0].temp,
        sugar:res.result.data[0].sugar
      })
      console.log(this.data.shopItem)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //增加数量
  add(){
    if(this.data.count < 20) {
      let count = this.data.count + 1
      //优惠价
      let allPrice = this.data.shopItem[0].newPrice * count
      //现场价
      let oldPrice = this.data.shopItem[0].oldPrice * count
      this.setData({
        count:count,
        allPrice:allPrice,
        oldPrice:oldPrice
      })
    }
  },
  //减少数量
  sub(){
    if(this.data.count > 1) {
      let count = this.data.count - 1
      //优惠价
      let allPrice = this.data.shopItem[0].newPrice * count
      //现场价
      let oldPrice = this.data.shopItem[0].oldPrice * count
      this.setData({
        count:count,
        allPrice:allPrice,
        oldPrice:oldPrice
      })
    } else {
      wx.switchTab({
        url: '/pages/menu/menu',
      })
    }
  },

  // 支付，把订单存到数据库
  goPay(){
    let allPrice = this.data.shopItem[0].newPrice * this.data.count
    let oldPrice = this.data.shopItem[0].oldPrice * this.data.count
    console.log(allPrice)
    let order = {
      allPrice:allPrice,
      oldPrice:oldPrice,
      price:this.data.shopItem[0].newPrice,
      title:this.data.shopItem[0].title,
      shopName:"瑞幸咖啡（宜宾大学城店）",
      num:this.data.count,
      img:this.data.shopItem[0].img,
      user_id:1,
      status:0,
      address:"四川轻化工大学",
      orderNum:"637452hgb2setu83hd6",
      sugar:this.data.sugar,
      temp:this.data.temp,
      time:formatTime(new Date())
    }
    console.log(order)
    wx.cloud.callFunction({
      name:"setOrder",
      data:{
        order:order
      }
    }).then(res => {
      //判断是否成功
      if(res.result._id != null && res.result._id != '') {
        wx.navigateTo({
          url: '/pages/confirmOrder/confirmOrder?user_id='+order.user_id,
        })
      } else {
        wx.showToast({
          title: '系统繁忙',
          icon:'error'
        })
      }
    })
  },
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})